package com.ysh.smartplatform.controller;

import com.ysh.smartplatform.dto.R;
import com.ysh.smartplatform.log.annotations.LogAnnotation;
import com.ysh.smartplatform.pojo.SysCategory;
import com.ysh.smartplatform.service.CategoryService;
import com.ysh.smartplatform.valigroup.Insert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 袁胜虎 on 2022/9/6 21:44
 */
@RestController
@RequestMapping("/categories")
@Api(tags = "分类管理模块")
public class CategoryController {

    private CategoryService categoryService;

    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    @PostMapping("/category")
    @ApiOperation("添加分类")
    @PreAuthorize("hasAnyAuthority('sys:category:add')")
    public R addCategory(@RequestBody @Validated(Insert.class)SysCategory category){
        categoryService.addCategory(category);
        return R.OK();
    }

    @GetMapping("")
    @ApiOperation("默认查询所有分类")
    @PreAuthorize("hasAnyAuthority('sys:category:chaxun')")
    public R findAllCategory(){
        return  R.OK(categoryService.findAllCategories());
    }

    @GetMapping("/category/{cId}")
    @ApiOperation("根据id查询分类")
    @LogAnnotation(title = "根据分类id查询",business_type = 0)
    @PreAuthorize("hasAnyAuthority('sys:category:one')")
    public R findCategoryById(@PathVariable Long cId){
        return R.OK(categoryService.findById(cId));
    }

    @DeleteMapping("/category/{cId}")
    @ApiOperation("根据id删除分类")
    public R deleteCategoryById(@PathVariable Long cId){
        categoryService.deleteById(cId);
        return R.OK();
    }
}
